package com.le.ui;

import com.le.component.BackGroundPanel;
import com.le.util.InsertDataToSql;
import com.le.util.Query;
import com.le.util.RandomAdd;
import com.le.util.ScreenUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;
import java.util.Map;

public class StudentAddPage {
    String hostIdAddress;
    JFrame jFrame = new JFrame("学生添加");
    BackGroundPanel backGroundPanel = new BackGroundPanel(new ImageIcon("images//背景-10.png").getImage());
    final int WIDTH = 720;
    final int HEIGHT = 480;
    Map<String, String[]> majors = RandomAdd.MAJOR;

    JLabel nameLabel = new JLabel("学 生 姓 名: ");
    JTextField nameTf = new JTextField();

    JLabel genderLabel = new JLabel("请选择性别:");
    JRadioButton male = new JRadioButton("男");
    JRadioButton female = new JRadioButton("女");

    JLabel facultyLabel = new JLabel("请选择院系:");
    JComboBox<String> facultyComboBox = new JComboBox<>(RandomAdd.FACULTY);

    JLabel majorLabel = new JLabel("请选择专业:");
    JComboBox<String> majorComboBox = new JComboBox<>(majors.get("信息技术与管理学院"));

    JLabel yearLabel = new JLabel("出生年:");
    JLabel monthLabel = new JLabel("出生月:");
    JLabel dayLabel = new JLabel("出生日:");
    JComboBox<String> yearsComboBox = new JComboBox<>();
    JComboBox<String> monthComboBox = new JComboBox<>();
    JComboBox<String> daysComboBox = new JComboBox<>();

    JButton add = new JButton("确      定");

    public StudentAddPage(String hostIdAddress) {
        this.hostIdAddress = hostIdAddress;
    }


    public void init() {
        //组装视图
        jFrame.setIconImage(new ImageIcon("images/添加学生.png").getImage());
        jFrame.setBounds((ScreenUtils.getScreenWidth() - WIDTH) / 2,
                (ScreenUtils.getScreenHeight() - HEIGHT) / 2, WIDTH, HEIGHT);

        Box nameBox = Box.createHorizontalBox();
        nameBox.add(nameLabel);
        nameBox.add(Box.createHorizontalStrut(10));
        nameBox.add(nameTf);

        Box genderChoiceBox = Box.createHorizontalBox();
        ButtonGroup genderButtonGroup = new ButtonGroup();
        genderButtonGroup.add(male);
        genderButtonGroup.add(female);
        genderChoiceBox.add(genderLabel);
        genderChoiceBox.add(Box.createHorizontalStrut(10));
        genderChoiceBox.add(male);
        genderChoiceBox.add(Box.createHorizontalStrut(10));
        genderChoiceBox.add(female);

        Box faultyBox = Box.createHorizontalBox();
        faultyBox.add(facultyLabel);
        faultyBox.add(Box.createHorizontalStrut(10));
        faultyBox.add(facultyComboBox);

        Box majorBox = Box.createHorizontalBox();
        majorBox.add(majorLabel);
        majorBox.add(Box.createHorizontalStrut(10));
        majorBox.add(majorComboBox);

        Box bornDayBox = Box.createHorizontalBox();
        for (int i = 1980; i < 2023; i++) {
            yearsComboBox.addItem(String.valueOf(i));
        }
        for (int i = 1; i < 13; i++) {
            monthComboBox.addItem(String.valueOf(i));
        }
        for (int i = 1; i < 32; i++) {
            daysComboBox.addItem(String.valueOf(i));
        }
        bornDayBox.add(yearLabel);
        bornDayBox.add(Box.createHorizontalStrut(5));
        bornDayBox.add(yearsComboBox);
        bornDayBox.add(Box.createHorizontalStrut(5));
        bornDayBox.add(monthLabel);
        bornDayBox.add(Box.createHorizontalStrut(5));
        bornDayBox.add(monthComboBox);
        bornDayBox.add(Box.createHorizontalStrut(5));
        bornDayBox.add(dayLabel);
        bornDayBox.add(Box.createHorizontalStrut(5));
        bornDayBox.add(daysComboBox);

        Box btnBox = Box.createHorizontalBox();
        add.setPreferredSize(new Dimension(80, 30));
        btnBox.add(add);

        Box content = Box.createVerticalBox();
        content.add(Box.createVerticalStrut(50));
        content.add(nameBox);
        content.add(Box.createVerticalStrut(20));
        content.add(faultyBox);
        content.add(Box.createVerticalStrut(20));
        content.add(majorBox);
        content.add(Box.createVerticalStrut(20));
        content.add(bornDayBox);
        content.add(Box.createVerticalStrut(20));
        content.add(genderChoiceBox);
        content.add(Box.createVerticalStrut(40));
        content.add(btnBox);

        backGroundPanel.add(content);

        jFrame.add(backGroundPanel);
        jFrame.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        jFrame.setVisible(true);

        //事件处理
        facultyComboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    String[] major = majors.get(facultyComboBox.getSelectedItem().toString());
                    majorComboBox.removeAllItems();
                    for (String majorItem : major) {
                        majorComboBox.addItem(majorItem);
                    }
                }
            }
        });

        add.addActionListener((ActionEvent e) -> {
            String newStudentName = nameTf.getText().trim();
            String newStudentFaulty = facultyComboBox.getSelectedItem().toString();
            String newStudentMajor = majorComboBox.getSelectedItem().toString();
            String newStudentGender = male.isSelected() ? "男" : "女";
            if (newStudentName.length() == 0) JOptionPane.showMessageDialog(jFrame, "请输入姓名！");
            else if (!(male.isSelected() || female.isSelected())) JOptionPane.showMessageDialog(jFrame, "请选择性别");
            else {
                Query query = new Query(hostIdAddress);
                query.setDatabaseName("sams");
                query.setSQL("select sno from student where major=" + "'" + newStudentMajor + "'");
                String[][] snoSet = query.getRecord();
                String endSno = snoSet[snoSet.length - 1][0];
                String newSno = Long.toString(Long.parseLong(endSno) + 1);
                String newStudentClass = newSno.substring(8, 10);
                String preSno = newSno.substring(0, 8);
                String newStudentLastTwoSno = newSno.substring(10, 12);
                if (Integer.parseInt(newStudentLastTwoSno) > 50) {
                    newStudentLastTwoSno = RandomAdd.getTwoNumber(Integer.parseInt(newStudentLastTwoSno) - 50);
                    newStudentClass = Integer.toString(Integer.parseInt(newStudentClass) + 1);
                }
                newSno = preSno + newStudentClass + newStudentLastTwoSno;
                String newStudentBornDay = yearsComboBox.getSelectedItem().toString() + "-" +
                        monthComboBox.getSelectedItem().toString() + "-" +
                        daysComboBox.getSelectedItem().toString();

                //插入新同学的信息到数据库
                String[][] newStudentData = new String[1][7];
                newStudentData[0][0] = newSno;
                newStudentData[0][1] = newStudentName;
                newStudentData[0][2] = newStudentGender;
                newStudentData[0][3] = newStudentFaulty;
                newStudentData[0][4] = newStudentMajor;
                newStudentData[0][5] = newStudentClass;
                newStudentData[0][6] = newStudentBornDay;
                try {
                    InsertDataToSql.InsertStudentDataRequest(hostIdAddress, "sams", newStudentData);
                    JOptionPane.showMessageDialog(jFrame, "插入成功！");
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}
